package com.zhoug.zmui.calendar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.RecyclerViewUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 可以限制最大宽高和最小宽高的RecyclerView
 *
 * @Author 35574
 * @Date 2022/4/29
 * @Description
 */
public class CalendarRecyclerView extends RecyclerView {
    private static final String TAG = ">>>CalendarRecyclerView";
    private static boolean DEBUG = false;
    private boolean mTouchPositionEnable = false;//是否支持获取触摸点的Item位置
    private GestureDetector mGestureDetector;
    private boolean isTouching = false;//正在长按
    private OnTouchPositionListener mOnTouchPositionListener;

    public CalendarRecyclerView(@NonNull Context context) {
        super(context);
        init();
    }

    public CalendarRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CalendarRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
            @Override
            public void onLongPress(MotionEvent e) {
                if (!isTouching) {
                    //长按启动
                    isTouching = true;
                    if (DEBUG) {
                        LogUtils.d(TAG, "onLongPress:true");

                    }
                }
                super.onLongPress(e);
            }
        });

    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        if (mTouchPositionEnable) {
            if (DEBUG) {
                LogUtils.d(TAG, "onInterceptTouchEvent:");
            }
            if (isTouching) {
                //拦截事件
                getParent().requestDisallowInterceptTouchEvent(true);
                if (mOnTouchPositionListener != null) {
                    mOnTouchPositionListener.onOpen(RecyclerViewUtils.getChildPositionByXY(this, e.getX(), e.getY()));
                }
                return true;
            }
            return mGestureDetector.onTouchEvent(e);
        }
        return super.onInterceptTouchEvent(e);
    }


    int lastPosition = -1;

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        if (mTouchPositionEnable && isTouching) {
            if (DEBUG) {
                LogUtils.d(TAG, "onTouchEvent:");
            }
            int action = e.getAction();
            float x = e.getX();
            float y = e.getY();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    if (DEBUG) {
                        LogUtils.d(TAG, "ACTION_DOWN:");
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (isTouching) {
                        getParent().requestDisallowInterceptTouchEvent(true);
                        if (DEBUG) {
                            LogUtils.d(TAG, "cutPosition:" + lastPosition);
                        }
                        int position = RecyclerViewUtils.getChildPositionByXY(this, x, y);
                        if (position != lastPosition && position != -1) {
                            lastPosition = position;
                            if (mOnTouchPositionListener != null) {
                                mOnTouchPositionListener.onMoveChange(lastPosition);
                            }
                        }

                    }
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    isTouching = false;
                    getParent().requestDisallowInterceptTouchEvent(false);
                    if (mOnTouchPositionListener != null) {
                        mOnTouchPositionListener.onClose(RecyclerViewUtils.getChildPositionByXY(this, x, y));
                    }
                    break;
            }
            return true;
        }
        return super.onTouchEvent(e);
    }

    public void setTouchPositionEnable(boolean touchPositionEnable) {
        this.mTouchPositionEnable = touchPositionEnable;
    }

    public void setOnTouchPositionListener(OnTouchPositionListener onTouchPositionListener) {
        this.mOnTouchPositionListener = onTouchPositionListener;
    }

    public interface OnTouchPositionListener {
        void onOpen(int position);
        void onMoveChange(int position);
        void onClose(int position);
    }


}
